/*
 *
 *  * Copyright 1999-2017 esfak47
 *  *
 *  * Licensed under the Apache License, Version 2.0 (the "License");
 *  * you may not use this file except in compliance with the License.
 *  * You may obtain a copy of the License at
 *  *
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  * Unless required by applicable law or agreed to in writing, software
 *  * distributed under the License is distributed on an "AS IS" BASIS,
 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  * See the License for the specific language governing permissions and
 *  * limitations under the License.
 *
 */

package com.esfak47.spring.boot.stater.avcloud.autoconfigure;

import com.avos.avoscloud.AVOSCloud;
import com.esfak47.spring.boot.stater.avcloud.properties.AVCloudPropeties;
import com.esfak47.spring.boot.stater.avcloud.service.AVCloudService;
import com.esfak47.spring.boot.stater.avcloud.service.AVCloudServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Tony
 * Created by tony on 2018/1/8.
 */
@Configuration
@ConditionalOnClass(AVCloudService.class)
@EnableConfigurationProperties(AVCloudPropeties.class)
public class AVCloudAutoConfiguration {
    @Autowired
    AVCloudPropeties avCloudPropeties;
    private Logger logger = LoggerFactory.getLogger(AVCloudAutoConfiguration.class);

    @Bean
    @ConditionalOnMissingBean(AVCloudService.class)
    public AVCloudService avCloudService() {
        logger.info("start init");
        AVOSCloud.initialize(avCloudPropeties.getAppId(), avCloudPropeties.getAppKey(), avCloudPropeties.getMasterKey());
        AVOSCloud.setDebugLogEnabled(avCloudPropeties.isDebug());
        if ("us".equals(avCloudPropeties.getNodeRegion())) {
            AVOSCloud.useAVCloudUS();
        } else {
            AVOSCloud.useAVCloudCN();
        }
        logger.info("init done");
        return new AVCloudServiceImpl();
    }
}
